package com.efast.cafe.portal.service.api.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.api.PortalApiModuleBean;
import com.efast.cafe.portal.bean.api.PortalApiObjectDto;
import com.efast.cafe.portal.bean.api.PortalApiProjectBean;
import com.efast.cafe.portal.bean.api.PortalApiProjectDto;
import com.efast.cafe.portal.bean.api.PortalApiServiceDto;
import com.efast.cafe.portal.bean.api.PortalApiServiceQueryBean;
import com.efast.cafe.portal.bean.api.PortalApiSystemBean;
import com.efast.cafe.portal.bean.api.PortalApiUpdateFaultDefBean;
import com.efast.cafe.portal.bean.api.PortalTreeNodeBean;
import com.efast.cafe.portal.dao.api.PortalApiModuleDao;
import com.efast.cafe.portal.dao.api.PortalApiObjectDao;
import com.efast.cafe.portal.dao.api.PortalApiProSysDefDao;
import com.efast.cafe.portal.dao.api.PortalApiProjectDao;
import com.efast.cafe.portal.dao.api.PortalApiServiceDao;
import com.efast.cafe.portal.dao.api.PortalApiSysModDefDao;
import com.efast.cafe.portal.dao.api.PortalApiSystemDao;
import com.efast.cafe.portal.dao.query.QueryTemplateDao;
import com.efast.cafe.portal.service.api.IPortalApiProjectService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * Service接口实现类
 * </p>
 * <p>
 * Table: portal_api_project - 所属项目定义
 * </p>
 *
 * @since 2017-08-08 11:45:28
 */
@Service
@Transactional
public class PortalApiProjectServiceImpl implements IPortalApiProjectService {

	private PortalApiProjectDao portalApiProjectDao;

	private PortalApiProSysDefDao portalApiProSysDefDao;

	private PortalApiSystemDao portalApiSystemDao;

	private PortalApiModuleDao portalApiModuleDao;

	private PortalApiObjectDao portalApiObjectDao;

	private PortalApiServiceDao portalApiServiceDao;

	private PortalApiSysModDefDao portalApiSysModDefDao;

	@SuppressWarnings("unused")
	private QueryTemplateDao queryTemplateDao;

	@Autowired
	public void setPortalApiProjectDao(PortalApiProjectDao portalApiProjectDao) {
		this.portalApiProjectDao = portalApiProjectDao;
	}

	@Autowired
	public void setPortalApiProSysDefDao(PortalApiProSysDefDao portalApiProSysDefDao) {
		this.portalApiProSysDefDao = portalApiProSysDefDao;
	}

	@Autowired
	public void setPortalApiSystemDao(PortalApiSystemDao portalApiSystemDao) {
		this.portalApiSystemDao = portalApiSystemDao;
	}

	@Autowired
	public void setPortalApiModuleDao(PortalApiModuleDao portalApiModuleDao) {
		this.portalApiModuleDao = portalApiModuleDao;
	}

	@Autowired
	public void setPortalApiObjectDao(PortalApiObjectDao portalApiObjectDao) {
		this.portalApiObjectDao = portalApiObjectDao;
	}

	@Autowired
	public void setPortalApiServiceDao(PortalApiServiceDao portalApiServiceDao) {
		this.portalApiServiceDao = portalApiServiceDao;
	}

	@Autowired
	public void setPortalApiSysModDefDao(PortalApiSysModDefDao portalApiSysModDefDao) {
		this.portalApiSysModDefDao = portalApiSysModDefDao;
	}

	@Autowired
	public void setQueryTemplateDao(QueryTemplateDao queryTemplateDao) {
		this.queryTemplateDao = queryTemplateDao;
	}

	/**
	 * 新增apiProject
	 */
	@Override
	public int insertApiProject(PortalApiProjectBean apiProjectBean) {
		// TODO Auto-generated method stub
		// apiProjectBean.setProjectName(apiProjectBean.getProjectName().replace("＂",
		// "\""));
		int count = portalApiProjectDao.projectVersionInsert(apiProjectBean);
		if (count != 0) {
			throw new ServiceException("版本重复，请重新输入版本");
		}
		apiProjectBeanCheck(apiProjectBean);
		int projectIndentifierCount = portalApiProjectDao.projectIndentifierCount(apiProjectBean);
		if (projectIndentifierCount != 0) {
			throw new ServiceException("系统标识符重复，请重新输入系统标识符");
		}
		apiProjectBean.setStatus((byte) 2);
		apiProjectBean.setUuid(UUID.randomUUID().toString());
		portalApiProjectDao.insertApiProject(apiProjectBean);
		return apiProjectBean.getId();
	}

	/**
	 * 修改apiProject
	 */
	@Override
	public void updateApiProject(PortalApiProjectBean apiProjectBean) {
		// TODO Auto-generated method stub
		// apiProjectBeanCheck(apiProjectBean);
		// apiProjectBean.setStatus((byte)2);
		// apiProjectBean.setProjectName(apiProjectBean.getProjectName().replace("＂",
		// "\""));
		/*
		 * int count = portalApiProjectDao.projectVersionUpdate(apiProjectBean);
		 * if(count!=0){ throw new ServiceException("版本重复，请重新输入版本"); }
		 */
		portalApiProjectDao.updateApiProject(apiProjectBean);
	}

	/**
	 * 删除apiProject
	 * 
	 */
	@Override
	public void deleteApiProject(PortalApiProjectBean apiProjectBean) {
		// TODO Auto-generated method stub
		// 判断该Project是否存在有效的system，存在的话不可删除
		int countSys = portalApiProSysDefDao.sysCountByProId(apiProjectBean.getId());
		if (countSys != 0) {
			throw new ServiceException("该项目存在有效系统，不可删除");
		}
		portalApiProjectDao.deleteApiProject(apiProjectBean);
	}

	/**
	 * 发布项目 {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月14日
	 * @see com.efast.cafe.portal.remote.api.PortalApiProjectService#publishApiProject(com.efast.cafe.portal.bean.api.PortalApiProjectBean)
	 *
	 */
	@Override
	public int publishApiProject(PortalApiProjectBean apiProjectBean) {
		// TODO Auto-generated method stub
		if (apiProjectBean.getSystemIds().isEmpty()) {
			throw new ServiceException("请选择需要发布的系统");
		}
		apiProjectBean.setStatus((byte) 1);
		apiProjectBean.setParentId(apiProjectBean.getProjectId());
		apiProjectBean.setPublishTime(new Date());
		apiProjectBean.setParentLastTime(apiProjectBean.getLastUpdateTime());
		apiProjectBean.setCreateUser(SpringWebUtils.getRequestUser());
		int count = portalApiProjectDao.projectVersionInsert(apiProjectBean);
		if (count != 0) {
			throw new ServiceException("版本重复，请重新输入版本");
		}
		// 发布项目，相当于复制项目
		portalApiProjectDao.upgradeApiProject(apiProjectBean);
		// 复制项目跟系统的关系
		portalApiProSysDefDao.publishApiProSysDef(apiProjectBean.getId(), apiProjectBean.getSystemIds(), SpringWebUtils.getRequestUser(), 0);
		return apiProjectBean.getId();
	}

	/**
	 * 升级项目 {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月15日
	 * @see com.efast.cafe.portal.remote.api.PortalApiProjectService#upgradeApiProject(com.efast.cafe.portal.bean.api.PortalApiProjectBean)
	 *
	 */
	@Override
	public int upgradeApiProject(PortalApiProjectBean apiProjectBean) {
		Integer oldProjectId = apiProjectBean.getProjectId();
		// TODO Auto-generated method stub
		// apiProjectBean.setParentId(apiProjectBean.getId());
		apiProjectBean.setStatus((byte) 2);
		apiProjectBean.setParentLastTime(apiProjectBean.getLastUpdateTime());
		apiProjectBean.setCreateUser(SpringWebUtils.getRequestUser());
		int count = portalApiProjectDao.projectVersionInsert(apiProjectBean);
		if (count != 0) {
			throw new ServiceException("版本重复，请重新输入版本");
		}
		// 升级项目，相当于复制项目
		portalApiProjectDao.upgradeApiProject(apiProjectBean);
		// 复制项目跟系统的关系
		portalApiProSysDefDao.upgradeApiProSysDef(apiProjectBean.getId(), oldProjectId, SpringWebUtils.getRequestUser());
		return apiProjectBean.getId();
	}

	/**
	 * 构造树 {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月18日
	 * @see com.efast.cafe.portal.remote.api.PortalApiProjectService#treeApiProject(com.efast.cafe.portal.bean.api.PortalApiProjectDto)
	 *
	 */
	@Override
	public List<PortalTreeNodeBean> treeApiProject(PortalApiProjectDto portalApiProjectDto) {
		// TODO Auto-generated method stub
		List<PortalTreeNodeBean> treeNodeBeans = new ArrayList<PortalTreeNodeBean>();
		// 查询树的节点，项目的时候，只传id,type为null
		if (portalApiProjectDto.getType() == null) {
			// 查询project
			List<PortalApiProjectBean> listProject = portalApiProjectDao.treeApiProject(portalApiProjectDto);
			for (int i = 0; i < listProject.size(); i++) {
				PortalTreeNodeBean bean = new PortalTreeNodeBean();
				bean.setData(listProject.get(i));
				bean.setLeaf(false);
				bean.setTitle(listProject.get(i).getProjectName());
				treeNodeBeans.add(bean);
			}
			// 父节点为project的时候，查询该父节点下的系统
		} else if (portalApiProjectDto.getType() != null && CommonConstant.PortalNodeType.PROJECR.equals(portalApiProjectDto.getType())) {
			// 查询system
			List<PortalApiSystemBean> listSyetem = portalApiSystemDao.queryPortalApiSystemsBy(portalApiProjectDto.getId(), null, null);
			for (int i = 0; i < listSyetem.size(); i++) {
				PortalTreeNodeBean bean = new PortalTreeNodeBean();
				bean.setData(listSyetem.get(i));
				bean.setLeaf(false);
				bean.setTitle(listSyetem.get(i).getSystemName());
				bean.setType(CommonConstant.PortalNodeType.SYSTEM);
				treeNodeBeans.add(bean);

			}
			// 父节点为system的时候，查询system下面的module
		} else if (portalApiProjectDto.getType() != null && CommonConstant.PortalNodeType.SYSTEM.equals(portalApiProjectDto.getType())) {
			// 查询module
			List<PortalApiModuleBean> listModule = portalApiModuleDao.treeApiModuleBySystemId(portalApiProjectDto);
			for (int i = 0; i < listModule.size(); i++) {
				PortalTreeNodeBean bean = new PortalTreeNodeBean();
				bean.setData(listModule.get(i));
				bean.setLeaf(false);
				bean.setTitle(listModule.get(i).getModuleName());
				bean.setType(CommonConstant.PortalNodeType.MODULE);
				treeNodeBeans.add(bean);

			}
			// 父节点是module的时候，固定出现对象列表，项目列表
		} else if (portalApiProjectDto.getType() != null && CommonConstant.PortalNodeType.MODULE.equals(portalApiProjectDto.getType())) {
			// 默认出现对象服务，列表服务的节点
			PortalTreeNodeBean bean = new PortalTreeNodeBean();
			bean.setData(portalApiProjectDto);
			bean.setLeaf(false);
			bean.setTitle("{\"en_US\":\"object list\",\"zh_CN\":\"对象列表\"}");
			bean.setType(CommonConstant.PortalNodeType.OBJECT);
			PortalTreeNodeBean bean1 = new PortalTreeNodeBean();
			bean1.setLeaf(false);
			bean1.setTitle("{\"en_US\":\"service list\",\"zh_CN\":\"服务列表\"}");
			bean1.setData(portalApiProjectDto);
			bean1.setType(CommonConstant.PortalNodeType.SERVICE);
			/*
			 * PortalTreeNodeBean bean2= new PortalTreeNodeBean();
			 * bean2.setLeaf(false);
			 * bean2.setTitle("{\"en_US\":\"query list\",\"zh_CN\":\"通用查询\"}");
			 * bean2.setData(portalApiProjectDto); bean2.setType(6);
			 * treeNodeBeans.add(2,bean2);
			 */
			treeNodeBeans.add(0, bean);
			treeNodeBeans.add(1, bean1);

			// 节点为object，查询module所属的object信息
		} else if (portalApiProjectDto.getType() != null && CommonConstant.PortalNodeType.OBJECT.equals(portalApiProjectDto.getType())) {
			PortalApiObjectDto portalApiObjectDto = new PortalApiObjectDto();
			portalApiObjectDto.setModuleId(portalApiProjectDto.getId());
			// 查询object
			List<PortalApiObjectDto> listObject = portalApiObjectDao.queryObjectByObjcetUUid(portalApiObjectDto);
			for (int i = 0; i < listObject.size(); i++) {
				PortalTreeNodeBean bean = new PortalTreeNodeBean();
				bean.setData(listObject.get(i));
				bean.setLeaf(true);
				bean.setType(CommonConstant.PortalNodeType.OBJECT);
				bean.setTitle(listObject.get(i).getObjName());
				treeNodeBeans.add(bean);
			}
			// 节点为service，查询module所属的object信息
		} else if (portalApiProjectDto.getType() != null && CommonConstant.PortalNodeType.SERVICE.equals(portalApiProjectDto.getType())) {
			PortalApiServiceQueryBean querybean = new PortalApiServiceQueryBean();
			querybean.setModuleId(portalApiProjectDto.getId());
			// 查询system
			List<PortalApiServiceDto> listService = portalApiServiceDao.queryPortalApiServiceList(querybean);
			for (int i = 0; i < listService.size(); i++) {
				PortalTreeNodeBean bean = new PortalTreeNodeBean();
				bean.setData(listService.get(i));
				bean.setLeaf(true);
				bean.setType(CommonConstant.PortalNodeType.SERVICE);
				bean.setTitle(listService.get(i).getServiceName());
				treeNodeBeans.add(bean);
			}
		}

		return treeNodeBeans;
	}

	/**
	 * 替换（查询除了当前项目版本之外的该项目的所有版本） {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月25日
	 * @see com.efast.cafe.portal.remote.api.PortalApiProjectService#queryProjectByIdAndUUid(com.efast.cafe.portal.bean.api.PortalApiProjectBean)
	 *
	 */
	@Override
	public List<PortalApiProjectBean> queryProjectByIdAndUUid(PortalApiProjectBean apiProjectBean) {
		// TODO Auto-generated method stub
		return portalApiProjectDao.queryProjectByIdAndUUid(apiProjectBean);
	}

	@Override
	public void updateDefaultDef(PortalApiUpdateFaultDefBean updateFaultDefBean) {
		// TODO Auto-generated method stub
		if (updateFaultDefBean.getType() != null && CommonConstant.PortalNodeType.PROJECR.equals(updateFaultDefBean.getType())) {
			/*
			 * portalApiProSysDefDao.updateApiProSysDefaultDef(updateFaultDefBean.
			 * getPid(), updateFaultDefBean.getOldId(), 1,
			 * SpringWebUtils.getRequestUser());
			 * portalApiProSysDefDao.updateApiProSysDefaultDef(updateFaultDefBean.
			 * getPid(), updateFaultDefBean.getNewId(), 0,
			 * SpringWebUtils.getRequestUser());
			 */
		} else if (updateFaultDefBean.getType() != null && CommonConstant.PortalNodeType.SYSTEM.equals(updateFaultDefBean.getType())) {
			portalApiProSysDefDao.updateApiProSysDefaultDef(updateFaultDefBean.getPid(), updateFaultDefBean.getOldId(), 1, SpringWebUtils.getRequestUser());
			portalApiProSysDefDao.updateApiProSysDefaultDef(updateFaultDefBean.getPid(), updateFaultDefBean.getNewId(), 0, SpringWebUtils.getRequestUser());
		} else if (updateFaultDefBean.getType() != null && CommonConstant.PortalNodeType.MODULE.equals(updateFaultDefBean.getType())) {
			portalApiSysModDefDao.updateIsDefault(updateFaultDefBean.getPid(), updateFaultDefBean.getOldId(), 1, SpringWebUtils.getRequestUser());
			portalApiSysModDefDao.updateIsDefault(updateFaultDefBean.getPid(), updateFaultDefBean.getNewId(), 0, SpringWebUtils.getRequestUser());
		}
	}

	/**
	 * 基础校验
	 * 
	 * @Date : 2017年9月1日
	 * @param apiProjectBean
	 */
	public void apiProjectBeanCheck(PortalApiProjectBean apiProjectBean) {
		if (apiProjectBean.getProjectName().isEmpty() || apiProjectBean.getVersion().isEmpty() || apiProjectBean.getIndentifier().isEmpty()) {
			throw new ServiceException("项目名称、版本、标识符不能为空！");
		}
	}


}